package org.mg.engine;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Create by bo.chen in 2018/9/28 下午8:29
 */
public class InstructFactory {

    private final static String packageName = "org.mg.engine.impl";

    private final static Map<String,Object> application = new ConcurrentHashMap<>();

    private final static Map<Class,Object> classApplication = new ConcurrentHashMap<>();

    static {
        try {
            if(null == application || application.size() == 0 || null == classApplication || classApplication.size() == 0) {
                Scanner.scan(packageName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public static Instruct getBean(String name) {
        Instruct instruct =  (Instruct) application.get(name + "Instruct");
        if(null == instruct) {
            return (Instruct) application.get("errorInstruct");
        }
        return instruct;
    }

    public static Instruct getBean(Class clazz) {
        Instruct instruct = (Instruct) application.get(clazz);
        if(null == instruct) {
            return (Instruct) application.get("error");
        }
        return instruct;
    }

    public static void setBean(String name,Object bean) {
        application.put(name,bean);
    }

    public static void setBean(Class clazz,Object bean) {
        classApplication.put(clazz,bean);
    }
}